﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.cms.businesslogic.datatype;
using System.Collections;
using umbraco.interfaces;

namespace MT.Flag.Umbraco.DataTypes.TabularData
{
    public class TabularDataAbstractDataEditor : AbstractDataEditor
    {

       



        public override Guid Id
        {
            get
            {
                return new Guid("5fd64849-4638-4daf-b293-1f79c221a863");
            }
        }


        public override string DataTypeName
        {
            get
            {
                return "Flag: Tabular Data";
            }
        }


        private TabularDataPrevalueEditor _prevalueeditor;

        public override umbraco.interfaces.IDataPrevalue PrevalueEditor
        {
            get
            {
                if (_prevalueeditor == null)
                    _prevalueeditor = new TabularDataPrevalueEditor(this);
                return _prevalueeditor;
            }
        }

        
        private TabularDataControl m_control = new TabularDataControl();

        public TabularDataAbstractDataEditor()
        {
            base.RenderControl = m_control;
            m_control.Init +=new EventHandler(m_control_Init);
            base.DataEditorControl.OnSave +=new AbstractDataEditorControl.SaveEventHandler(DataEditorControl_OnSave);
        }


        void m_control_Init(object sender, EventArgs e)
        {
            // Get configuration

            SortedList s = ((TabularDataPrevalueEditor)PrevalueEditor).StoredConfiguration;

            // Set configuration
            if (s.ContainsKey("rows"))
            {
                int _rows;
                if (Int32.TryParse(s.GetByIndex(s.IndexOfKey("rows")).ToString(), out _rows))
                    m_control.Rows = _rows;
            }

            if (s.ContainsKey("cols"))
            {
                int _cols;
                if (Int32.TryParse(s.GetByIndex(s.IndexOfKey("cols")).ToString(), out _cols))
                    m_control.Columns = _cols;
            }

            if (s.ContainsKey("headercol"))
                m_control.HeaderColumn = s.GetByIndex(s.IndexOfKey("headercol")).ToString() == "1";

            if (s.ContainsKey("headerrow"))
                m_control.HeaderRow = s.GetByIndex(s.IndexOfKey("headerrow")).ToString() == "1";

            if (s.ContainsKey("footerrow"))
                m_control.FooterRow = s.GetByIndex(s.IndexOfKey("footerrow")).ToString() == "1";


            if (s.ContainsKey("headerwidth"))
            {
                int _headerwidth;
                if (Int32.TryParse(s.GetByIndex(s.IndexOfKey("headerwidth")).ToString(), out _headerwidth))
                    m_control.HeaderWidth = _headerwidth;
            }

            if (s.ContainsKey("defaultwidth"))
            {
                int _defaultwidth;
                if (Int32.TryParse(s.GetByIndex(s.IndexOfKey("defaultwidth")).ToString(), out _defaultwidth))
                    m_control.DefaultWidth = _defaultwidth;
            }



            if (this.Data.Value != null)
                this.m_control.Value = this.Data.Value.ToString();



        }


        void DataEditorControl_OnSave(EventArgs e)
        {
            this.Data.Value = m_control.Value;            
        }


        private IData m_Data;

        public override umbraco.interfaces.IData Data
        {
            get
            {
                if (this.m_Data == null)
                    this.m_Data = new TabularDataData(this);

                return this.m_Data;
            }
        }

    }
}
